package com.domino.server.persistence.gamesession;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.persistence.EntityManager;
import javax.persistence.OptimisticLockException;
import javax.persistence.Query;
import javax.persistence.TemporalType;

import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheManager;

import com.domino.server.cache.CachedBoard;
import com.domino.server.cache.GameSessionData;
import com.domino.server.cache.State;
import com.domino.server.persistence.EMF;
import com.domino.shared.model.Board;
import com.domino.shared.model.SimpleBoard;
import com.domino.shared.session.SessionId;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class BoardDaoImpl implements BoardDao {

	private static final String BOARD_CACHE_NAME = "board.5";
	Logger LOG = Logger.getLogger(getClass().getCanonicalName());
	private Cache cache;
	
	public BoardDaoImpl() {
		try {
			cache = CacheManager.getInstance().getCache(BOARD_CACHE_NAME);
			if (cache == null) {
				cache = CacheManager.getInstance().getCacheFactory()
						.createCache(Collections.emptyMap());
				CacheManager.getInstance().registerCache(BOARD_CACHE_NAME, cache);
			}

		} catch (CacheException e) {
			throw new RuntimeException("Failed to create cache", e);
		}
	}



	@Override
	public CachedBoard find(SessionId sessionId) {
		CachedBoard board = (CachedBoard) cache.get(sessionId);
		return board;
	}



	@Override
	public void update(CachedBoard board) {
		cache.put(board.getSessionId(), board);
	}




}
